#include "Number.h"

void
number_init (Number* obj)
{
        object_init (OBJECT (obj));
        object_set_destory_func (obj, number_destory);
        obj->type           = N_TYPE_INTEGER;
        obj->number.f       = 0.0;
        obj->multiple_digit = 0.0;
}

void
number_destruction (Number* obj)
{}

OBJECT_DEFINE_START (Number, number)

void
number_copy_to (Number* obj, Number* dest_obj)
{
        if (!obj || !dest_obj)
                return;
        /*仅复制类型和数字本身*/
        dest_obj->type     = obj->type;
        dest_obj->number.f = obj->number.f;
}

void
number_change_type (Number* obj, NType type)
{
        if (!obj || !type)
                return;
        if (obj->type == type)
                return;
        if (obj->type == N_TYPE_INTEGER) {
                obj->type     = N_TYPE_DECIMAL;
                obj->number.f = (decimal_t)obj->number.l;
        } else {
                obj->type     = N_TYPE_INTEGER;
                obj->number.l = (uint_max)obj->number.f;
        }
}

void
number_pull (Number* obj, uint8 num)
{
        if (!obj)
                return;
        /*添加数字到整数*/
        if (obj->type == N_TYPE_INTEGER) {
                obj->number.l *= 10;
                obj->number.l += num;
                return;
        }

        /*添加数字到小数，multiple_digit 是当前倍率*/
        if (obj->multiple_digit == 0.0)
                obj->multiple_digit = 10.0;
        else
                obj->multiple_digit *= 10.0;
        obj->number.f += ((decimal_t)num / obj->multiple_digit);
}